package com.castle.fortress.admin.member.rest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.annotation.CastleLog;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.member.dto.MemberDto;
import com.castle.fortress.admin.member.dto.MemberThumbsUpDto;
import com.castle.fortress.admin.member.entity.MemberThumbsUpEntity;
import com.castle.fortress.admin.member.service.MemberThumbsUpService;
import com.castle.fortress.admin.utils.WebUtil;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.enums.OperationTypeEnum;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.BizErrorCode;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.utils.CommonUtil;
import com.castle.fortress.common.utils.ConvertUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 会员点赞表 api 控制器
 *
 * @author castle
 * @since 2023-02-20
 */
@Api(tags="会员点赞表api管理控制器")
@RestController
@RequestMapping("/api/member/memberThumbsUp")
public class ApiMemberThumbsUpController {
    @Autowired
    private MemberThumbsUpService memberThumbsUpService;


    /**
     * 会员点赞表的分页展示
     * @param memberThumbsUpDto 会员点赞表实体类
     * @param currentPage 当前页
     * @param size  每页记录数
     * @return
     */
    @CastleLog(operLocation="会员点赞表-api",operType = OperationTypeEnum.QUERY)
    @ApiOperation("会员点赞表分页展示")
    @GetMapping("/page")
    public RespBody<IPage<MemberThumbsUpDto>> pageMemberThumbsUp(MemberThumbsUpDto memberThumbsUpDto, @RequestParam(required = false) Integer currentPage, @RequestParam(required = false)Integer size){
        Integer pageIndex= currentPage==null? GlobalConstants.DEFAULT_PAGE_INDEX:currentPage;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<MemberThumbsUpDto> page = new Page(pageIndex, pageSize);

        IPage<MemberThumbsUpDto> pages = memberThumbsUpService.pageMemberThumbsUp(page, memberThumbsUpDto);
        return RespBody.data(pages);
    }

    /**
     * 会员点赞
     * @param memberThumbsUpDto 会员点赞表实体类
     * @return
     */
    @CastleLog(operLocation="会员点赞表-api",operType = OperationTypeEnum.INSERT)
    @ApiOperation("会员点赞表保存")
    @PostMapping("/save")
    public RespBody<String> saveMemberThumbsUp(@RequestBody MemberThumbsUpDto memberThumbsUpDto){
        MemberDto member = WebUtil.currentMember();
        if(member==null){
            throw new BizException(GlobalRespCode.NO_LOGIN_ERROR);
        }
        if(memberThumbsUpDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        QueryWrapper<MemberThumbsUpEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("member_id",member.getId());
        wrapper.eq("target_id",memberThumbsUpDto.getTargetId());
        List<MemberThumbsUpEntity> list = memberThumbsUpService.list(wrapper);
        if(list!=null && list.size()>0){
            throw new BizException(BizErrorCode.MEMBER_THUMPS_UP_REPEAT);
        }
        MemberThumbsUpEntity memberThumbsUpEntity = ConvertUtil.transformObj(memberThumbsUpDto,MemberThumbsUpEntity.class);
        memberThumbsUpEntity.setMemberId(member.getId());
        memberThumbsUpEntity.setCreateTime(new Date());
        if(memberThumbsUpService.saveMemberThumbsUp(memberThumbsUpEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 会员点赞表编辑
     * @param memberThumbsUpDto 会员点赞表实体类
     * @return
     */
    @CastleLog(operLocation="会员点赞表-api",operType = OperationTypeEnum.UPDATE)
    @ApiOperation("会员点赞表编辑")
    @PostMapping("/edit")
    public RespBody<String> updateMemberThumbsUp(@RequestBody MemberThumbsUpDto memberThumbsUpDto){
        if(memberThumbsUpDto == null || memberThumbsUpDto.getId() == null || memberThumbsUpDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            MemberThumbsUpEntity memberThumbsUpEntity = ConvertUtil.transformObj(memberThumbsUpDto,MemberThumbsUpEntity.class);
        if(memberThumbsUpService.updateById(memberThumbsUpEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 会员点赞取消
     * @param memberThumbsUpDto 会员点赞对象
     * @return
     */
    @CastleLog(operLocation="会员点赞表-api",operType = OperationTypeEnum.DELETE)
    @ApiOperation("会员点赞表删除")
    @PostMapping("/cancel")
    public RespBody<String> cancelMemberThumbsUp(@RequestBody MemberThumbsUpDto memberThumbsUpDto){
        MemberDto member = WebUtil.currentMember();
        if(member==null){
            throw new BizException(GlobalRespCode.NO_LOGIN_ERROR);
        }
        if(CommonUtil.verifyParamNull(memberThumbsUpDto,memberThumbsUpDto.getTargetId(),memberThumbsUpDto.getType())){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        memberThumbsUpDto.setMemberId(member.getId());
        QueryWrapper<MemberThumbsUpEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("member_id",member.getId());
        wrapper.eq("target_id",memberThumbsUpDto.getTargetId());
        List<MemberThumbsUpEntity> list = memberThumbsUpService.list(wrapper);
        if(list==null || list.size()!=1){
            throw new BizException(GlobalRespCode.DB_DATA_ERROR);
        }
        memberThumbsUpDto.setId(list.get(0).getId());
        if(memberThumbsUpService.cancelMemberThumbsUp(memberThumbsUpDto)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 会员点赞表详情
     * @param id 会员点赞表id
     * @return
     */
    @CastleLog(operLocation="会员点赞表-api",operType = OperationTypeEnum.QUERY)
    @ApiOperation("会员点赞表详情")
    @GetMapping("/info")
    public RespBody<MemberThumbsUpDto> infoMemberThumbsUp(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            MemberThumbsUpEntity memberThumbsUpEntity = memberThumbsUpService.getById(id);
            MemberThumbsUpDto memberThumbsUpDto = ConvertUtil.transformObj(memberThumbsUpEntity,MemberThumbsUpDto.class);
        return RespBody.data(memberThumbsUpDto);
    }


}
